<template>
	<view class="parent">
		<view class="top">
			<image class="avatar" :src="avatar"></image>
			<view class="tip">{{tip}}</view>
		</view>

		<view class="content">
			<view class="tag">
				<view class="label">会员特权</view>
				<view class="list">
					<view class="item" v-for="(item, index) in tags" :key="index">
						<image :src="item.pic"></image>
						<view class="title">{{item.title}}</view>
					</view>
				</view>
			</view>

			<view class="open">
				<view class="label">会员开通</view>
				<view class="list">
					<view class="item" :class="{active: selectIndex == index}" v-for="(item, index) in list"
						:key="index" @click="onItem(item, index)">
						<view class="month" :class="{active: selectIndex == index}">{{item.num}}个月</view>
						<view class="price" :class="{active: selectIndex == index}">
							￥
							<view class="big" :class="{active: selectIndex == index}">{{item.priceDay}}</view>
							/天
						</view>
						<view class="money" :class="{active: selectIndex == index}">￥{{item.price}}</view>
					</view>
				</view>
			</view>
		</view>

		<view class="bottom" @click="showDialogPay">￥{{selectItem.price}}立即开通</view>

		<u-popup v-model="payVisiable" mode="bottom" border-radius="30" height="450" :closeable="true">
			<view class="popup">
				<view class="title">
					选择支付方式
				</view>
				<view class="type" v-for="(item, index) in payTypes" :key="index" @click="onItemPay(index)">
					<image :src="item.icon"></image>
					<view class="tip">{{item.title}}</view>
					<u-checkbox-group>
						<u-checkbox v-model="item.checked" shape="circle"></u-checkbox>
					</u-checkbox-group>
				</view>
				<view>
					<u-button type="primary" @click="requestPay">确定支付</u-button>
				</view>
			</view>
		</u-popup>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				first: true,
				avatar: '',
				tip: '您尚未开通VIP会员',
				tags: [{
						pic: this.getImageUrl('vip_icon1.png'),
						title: '免费畅聊'
					},
					{
						pic: this.getImageUrl('vip_icon2.png'),
						title: '会员专属色'
					},
					{
						pic: this.getImageUrl('vip_icon3.png'),
						title: '查看谁看到我'
					},
					{
						pic: this.getImageUrl('vip_icon4.png'),
						title: '专属标识'
					},
					{
						pic: this.getImageUrl('vip_icon5.png'),
						title: '专属气泡'
					},
					{
						pic: this.getImageUrl('vip_icon6.png'),
						title: '专属vip客服'
					},
				],
				list: [],
				selectIndex: 0,
				selectItem: {},
				payTypes: [{
						payType: 1,
						icon: this.getImageUrl('pay_alipay.png'),
						title: '支付宝支付',
						checked: false
					},
					{
						payType: 2,
						icon: this.getImageUrl('pay_wechat.png'),
						title: '微信支付',
						checked: false
					},
				],
				payVisiable: false,
				payType: '',
			}
		},
		onLoad() {
			this.avatar = uni.getStorageSync("userPic");
		},
		onShow() {
			this.requestVipList();
		},
		methods: {
			onItem(item, index) {
				this.selectItem = item;
				this.selectIndex = index;
			},
			showDialogPay() {
				this.payVisiable = true
			},
			onItemPay(index) {
				this.payTypes.forEach(item => {
					item.checked = false
				});
				this.payTypes[index].checked = true;
			},

			async requestVipList() {
				let res = await this.$common.post('/app/vip/list');
				if (0 == res.code) {
					if (this.first) {
						this.list = res.data.list;
						this.selectItem = res.data.list[0];
						this.first = false;
					}
					if (1 == res.data.user.isVip) {
						this.tip = '已开通VIP,' + res.data.user.vipEndDate + '到期';
					}
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			async requestPay() {
				this.payVisiable = false;
				let payType = '';
				this.payTypes.forEach(item => {
					if (item.checked) {
						payType = item.payType;
					}
				});
				if (!payType) {
					uni.showToast({
						title: '请选择支付类型',
						icon: 'none'
					})
					return
				}

				let url = '/api/aliPayVersion3/transactionsH5';
				if (2 == payType) {
					url = '/api/wechatVersion3/rechargeH5';
				}

				const params = {
					type: '2',
					projectId: this.selectItem.id
				}
				let res = await this.$common.post(url, params);
				if (0 == res.code) {
					const webview = plus.webview.create('', 'custom-webview');
					webview.loadURL(res.data.payUrl, {
						'Referer': 'http://api.ptszyxx.com://'
					});
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			requestPayMpwachat() {
				const wxappid = 'wxff095378edaf00da';
				const secret = '827bef592cc5acb1e34351ba0796b62d';
				uni.request({
					url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${wxappid}&secret=${secret}`,
					success: (res) => {
						let token = res.data.access_token;
						let userId = uni.getStorageSync('userId');
						let querystr = `userId=${userId}&price=${this.selectItem.price}&type=2&projectId=${this.selectItem.id}`;
						let param = {
							path: 'pages/m-pages/m-personnelList/emptypay',
							query: querystr,
							env_version: 'release', //正式版为"release"，体验版为"trial"，开发版为"develop"
							expire_type: 1,
							expire_interval: 30,
						}
						uni.request({
							url: `https://api.weixin.qq.com/wxa/generate_urllink?access_token=${token}`,
							method: 'POST',
							data: param,
							success(result) {
								console.log("result: " + JSON.stringify(result));
								const webview = plus.webview.create('', 'custom-webview');
								webview.loadURL(result.data.url_link, {
									'Referer': 'http://api.ptszyxx.com://'
								});
							}
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.parent {
		display: flex;
		flex-direction: column;
		width: 100wh;
		height: 100vh;
		box-sizing: border-box;
		padding: 20rpx;
		align-items: center;

		.top {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 20rpx;

			.avatar {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}

			.tip {
				padding: 15rpx 25rpx;
				background-color: #fefefe;
				border-radius: 50rpx;
				margin-top: 20rpx;
				font-size: 24rpx;
				color: #555;
			}
		}

		.content {
			flex: 1;

			.tag {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 30rpx;
				background-color: #fff;
				border-radius: 15rpx;
				padding: 30rpx;

				.label {
					font-size: 28rpx;
					color: #ab7aff;
					font-weight: bold;
				}

				.list {
					display: flex;
					flex-direction: row;
					flex-wrap: wrap;
					width: 100%;

					.item {
						display: flex;
						flex-direction: column;
						align-items: center;
						width: 33.3%;
						margin: 15rpx 0;

						image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
						}

						.title {
							font-size: 24rpx;
							color: #888;
							margin-top: 15rpx;
						}
					}
				}
			}

			.open {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-top: 30rpx;
				background-color: #fff;
				border-radius: 15rpx;
				padding: 30rpx;

				.label {
					font-size: 28rpx;
					color: #ab7aff;
					font-weight: bold;
				}

				.list {
					display: flex;
					flex-direction: row;
					align-content: space-around;
					flex-wrap: wrap;
					width: 100%;

					.item {
						display: flex;
						flex-direction: column;
						width: 30%;
						margin: 15rpx 1.5%;
						border: 1rpx solid #ab7aff;
						box-sizing: border-box;
						border-radius: 20rpx;
						padding: 20rpx;
						background-color: white;

						.month {
							font-size: 24rpx;
							color: #999;
						}

						.month.active {
							color: #fff;
						}

						.price {
							display: flex;
							flex-direction: row;
							align-items: flex-end;
							font-size: 24rpx;
							color: #ab7aff;
							margin: 30rpx 0;

							.big {
								font-size: 36rpx;
							}
						}

						.price.active {
							color: #fff;
						}

						.money {
							font-size: 28rpx;
							color: #555;
						}

						.money.active {
							color: #fff;
						}
					}

					.item.active {
						background-image: linear-gradient(to bottom, #9fe9ff, #ab7aff);
					}
				}
			}
		}

		.bottom {
			margin: 20rpx 50rpx;
			box-sizing: border-box;
			background-image: linear-gradient(to right, #9fe9ff, #ab7aff);
			border-radius: 40rpx;
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			color: white;
		}
	}

	.popup {
		display: flex;
		flex-direction: column;
		justify-content: center;
		width: 100wh;
		height: 100%;
		padding: 30rpx;

		.title {
			font-size: 36rpx;
			color: #222;
			line-height: 60rpx;
			height: 60rpx;
			text-align: center;
		}

		.type {
			flex: 1;
			display: flex;
			flex-direction: row;
			align-items: center;
			width: 100%;
			height: 100rpx;
			font-size: 28rpx;
			color: #222;

			image {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}

			.tip {
				flex: 1;
			}

		}
	}
</style>